package ui.consultas;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Point;

import javax.swing.JPanel;
import javax.swing.JInternalFrame;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.io.File;
import java.io.IOException;
import java.lang.StringBuffer;
import java.util.Date;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import java.text.FieldPosition;
import java.lang.String;
import java.text.ParsePosition;
import com.toedter.calendar.JDateChooser;

import dominio.Cliente;
import dominio.Contenedora;
import dominio.Zona;
import dominio.Venta;
import ui.abm.UIABMVenta;
import ui.componentes.SwingWorker;
import ui.tableModels.TableModelVentas;

import java.util.GregorianCalendar;
import java.awt.Font;
import javax.swing.JComboBox;

public class UIConsultaVentas extends JInternalFrame {

	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JTable tablaventas = null;
	private JButton btnfilter = null;
	private JButton btncerrar = null;
	private JLabel jLabel = null;
	private JTextField txtcliente = null;
	private JLabel jLabel1 = null;
	private JDateChooser fchdesde = null;
	private JLabel jLabel11 = null;
	private JDateChooser fchhasta = null;
	
	private TableModelVentas modeloVentas;
	private ArrayList colVentas;
	private TableRowSorter sorter;
	private JLabel jLabel2 = null;
	private JComboBox cmbzonas = null;
	private JButton btnnuevo = null;
	private JButton btnmodificar = null;
	
	/**
	 * This is the xxx default constructor
	 */
	public UIConsultaVentas() {
		super();
		initialize();
		GregorianCalendar calendario = new GregorianCalendar();
		fchhasta.setDate(calendario.getTime());
		calendario.add(GregorianCalendar.DATE, -7);
		fchdesde.setDate(calendario.getTime());
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(582, 433);
		this.setFrameIcon(new ImageIcon(getClass().getResource("/imagenes/signodolar.gif")));
		this.setClosable(true);
		this.setTitle("Consulta de Ventas");
		this.setContentPane(getJContentPane());
		this.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() {
			public void internalFrameOpened(javax.swing.event.InternalFrameEvent e) {
				
				modeloVentas = new TableModelVentas();
				tablaventas.setModel(modeloVentas);
				TableColumn column = null;
				for (int i = 0; i < 6; i++) {
				    column = tablaventas.getColumnModel().getColumn(i);
				    javax.swing.table.DefaultTableCellRenderer CellRenderer = new javax.swing.table.DefaultTableCellRenderer();
				    switch (i)
			        {
			            case 0:
					        column.setPreferredWidth(20); //ID
					        break;
			            case 1:
					        column.setPreferredWidth(50); //Fecha
					        break;
			            case 2:
					        column.setPreferredWidth(120); //Cliente
					        break;
			            case 3:
					        column.setPreferredWidth(70); //Zona
					        break;
			            case 4:
					        column.setPreferredWidth(50); //Total
					        break;
			            case 5:
					        column.setPreferredWidth(50); //Entrega
					        break;
			        }
				}
				Iterator it = Contenedora.getInstancia().obtenerZonas().iterator();
				while (it.hasNext()){
					Zona unaZona = (Zona) it.next();	
					cmbzonas.addItem(unaZona);
				}
				
				sorter = new TableRowSorter(modeloVentas);
				tablaventas.setRowSorter(sorter);
				tablaventas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				
				Zona unaZona = new Zona();
				unaZona.setId(0);
				unaZona.setDescripcion("Todas");
				cmbzonas.insertItemAt(unaZona,0);
				cmbzonas.setSelectedItem(unaZona);
				
				//actualizoTabla();
			}
		});
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(14, 87, 39, 16));
			jLabel2.setText("Zona:");
			jLabel11 = new JLabel();
			jLabel11.setBounds(new Rectangle(443, 58, 19, 16));
			jLabel11.setText("  Y");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(307, 57, 36, 16));
			jLabel1.setText("Entre:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(14, 57, 48, 16));
			jLabel.setText("Cliente:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getBtnfilter(), null);
			jContentPane.add(getBtncerrar(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getTxtcliente(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getFchdesde(), null);
			jContentPane.add(jLabel11, null);
			jContentPane.add(getFchhasta(), null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getCmbzonas(), null);
			jContentPane.add(getBtnnuevo(), null);
			jContentPane.add(getBtnmodificar(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(0, 111, 570, 286));
			jScrollPane.setViewportView(getTablaventas());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes tablaventas	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTablaventas() {
		if (tablaventas == null) {
			tablaventas = new JTable();
			tablaventas.setFont(new Font("Dialog", Font.PLAIN, 12));
		}
		return tablaventas;
	}

	/**
	 * This method initializes btnfilter	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnfilter() {
		if (btnfilter == null) {
			btnfilter = new JButton();
			btnfilter.setBounds(new Rectangle(8, 10, 106, 35));
			btnfilter.setText("");
			btnfilter.setIcon(new ImageIcon(getClass().getResource("/imagenes/filtrar.jpg")));
			btnfilter.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SwingWorker worker = new SwingWorker() {
						public Object construct() {
							actualizoTabla();
							return null;
						}
						public void finished() {
							setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
						}

					};
					worker.start();
					setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				}
			});
		}
		return btnfilter;
	}

	/**
	 * This method initializes btncerrar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtncerrar() {
		if (btncerrar == null) {
			btncerrar = new JButton();
			btncerrar.setBounds(new Rectangle(347, 10, 106, 35));
			btncerrar.setIcon(new ImageIcon(getClass().getResource("/imagenes/cerrar.jpg")));
			btncerrar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return btncerrar;
	}
	
	public void actualizoTabla(){
		Zona unaZona = (Zona)cmbzonas.getSelectedItem();
		if (unaZona.getId() == 0){
			unaZona = null;
		}
		colVentas = Contenedora.getInstancia().buscoVentas(txtcliente.getText(), fchdesde.getDate(), fchhasta.getDate(), unaZona);
		modeloVentas.setColVentas(colVentas);
		List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(6);
		RowFilter<Object,Object> filtros = RowFilter.andFilter(filters);
		sorter.setRowFilter(filtros);
	}

	/**
	 * This method initializes txtcliente	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtcliente() {
		if (txtcliente == null) {
			txtcliente = new JTextField();
			txtcliente.setBounds(new Rectangle(66, 55, 210, 20));
		}
		return txtcliente;
	}

	/**
	 * This method initializes fchdesde	
	 * 	
	 * @return com.toedter.calendar.JDateChooser	
	 */
	private JDateChooser getFchdesde() {
		if (fchdesde == null) {
			fchdesde = new JDateChooser();
			fchdesde.setBounds(new Rectangle(347, 54, 95, 22));
			fchdesde.setFont(new Font("Arial", Font.PLAIN, 12));
			fchdesde.setDateFormatString("dd/MM/yyyy");
		}
		return fchdesde;
	}

	/**
	 * This method initializes fchhasta	
	 * 	
	 * @return com.toedter.calendar.JDateChooser	
	 */
	private JDateChooser getFchhasta() {
		if (fchhasta == null) {
			fchhasta = new JDateChooser();
			fchhasta.setBounds(new Rectangle(466, 54, 95, 22));
			fchhasta.setDateFormatString("dd/MM/yyyy");
		}
		return fchhasta;
	}

	/**
	 * This method initializes cmbzonas	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCmbzonas() {
		if (cmbzonas == null) {
			cmbzonas = new JComboBox();
			cmbzonas.setBounds(new Rectangle(67, 84, 209, 20));
		}
		return cmbzonas;
	}

	/**
	 * This method initializes btnnuevo	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnnuevo() {
		if (btnnuevo == null) {
			btnnuevo = new JButton();
			btnnuevo.setBounds(new Rectangle(121, 10, 106, 35));
			btnnuevo.setIcon(new ImageIcon(getClass().getResource("/imagenes/nuevo.jpg")));
			btnnuevo.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Venta unaVenta = new Venta();
					abmVenta(unaVenta);
				}
			});
		}
		return btnnuevo;
	}

	/**
	 * This method initializes btnmodificar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnmodificar() {
		if (btnmodificar == null) {
			btnmodificar = new JButton();
			btnmodificar.setBounds(new Rectangle(234, 10, 106, 35));
			btnmodificar.setIcon(new ImageIcon(getClass().getResource("/imagenes/modificar.jpg")));
			btnmodificar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ListSelectionModel rowSM = tablaventas.getSelectionModel();
					if(rowSM.isSelectionEmpty() != true){
						Venta unaVenta = modeloVentas.getVenta(tablaventas.convertRowIndexToModel(rowSM.getMinSelectionIndex()));
						Contenedora.getInstancia().cargoVentaConLineas(unaVenta);
						abmVenta(unaVenta);
					}
				}
			});
		}
		return btnmodificar;
	}
	
	public void abmVenta(Venta unaVenta){
		UIABMVenta frmVenta = new UIABMVenta(unaVenta);
		//UIConsultaClientes frmclientes = new UIConsultaClientes();
		getParent().add(frmVenta);
		frmVenta.setVisible(true);
		Dimension dim = frmVenta.getSize();
		Dimension bigDim = getParent().getSize();
		Point loc = getParent().getLocation();
		int xPos = ((int)bigDim.getWidth() - (int)dim.getWidth()) / 2;
		int yPos = ((int)bigDim.getHeight() - (int)dim.getHeight()) / 2;
		frmVenta.setLocation((int)loc.getX() + xPos, (int)loc.getY() + yPos);
	}

}  //  @jve:decl-index=0:visual-constraint="-6,-29"
